<?php

/**
* AdminMenuTest.php
*
* Guid add test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';


require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';

require_once '../framework/AuthSingleton.php';

class  AdminAuthTest extends WebTestCase
{

    private $testCodeName,$testSlideName,$testDesc,$testCodeId,$testSlideId,$url;
    private $username = '';
    private $password = '';
    private $dbh;


    public function setUp() 
    {
        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";

        $this->userUsername = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->userPassword = "robota";        

        $this->testCodeName = md5(uniqid(rand(), true));
        $this->testSlideName = md5(uniqid(rand(), true));
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url = $global['service_url'];

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $this->dbh = new PDO($options['dsn_string']); 
        $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );


        AuthSingleton::instance()->addUser($this->username,$this->password);
        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413");
    }

    public function testAddUser()
    {
        //Go to default page
        $this->get($this->url.'?');
        $this->click("Nowy użytkownik");
        $this->assertText('Podaj login i hasło. Użyj działającego adresu e-mail jako login.');
        $this->setFieldByName('username', $this->userUsername);
        $this->setFieldByName('password', $this->userPassword);
        $this->setFieldByName('passwordRepeat', $this->userPassword);
        $this->click("Dodaj");
        $this->assertText('Użytkownik dodany.');
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $sth = $this->dbh->prepare("SELECT COUNT(ROWID) FROM auth_user WHERE user = :user");
        $sth -> bindParam(':user',$this->userUsername,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
    }

    public function testAddUserExists()
    {
        //Go to default page
        $this->get($this->url.'?');
        $this->click("Nowy użytkownik");
        $this->assertText('Podaj login i hasło. Użyj działającego adresu e-mail jako login.');
        $this->setFieldByName('username', $this->userUsername);
        $this->setFieldByName('password', $this->userPassword);
        $this->setFieldByName('passwordRepeat', $this->userPassword);
        $this->click("Dodaj");
        $this->assertText('Użytkownik dodany.');
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $sth = $this->dbh->prepare("SELECT COUNT(ROWID) FROM auth_user WHERE user = :user");
        $sth -> bindParam(':user',$this->userUsername,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);

        $this->get($this->url.'?');
        $this->click("Nowy użytkownik");
        $this->assertText('Podaj login i hasło. Użyj działającego adresu e-mail jako login.');
        $this->setFieldByName('username', $this->userUsername);
        $this->setFieldByName('password', $this->userPassword);
        $this->setFieldByName('passwordRepeat', $this->userPassword);
        $this->click("Dodaj");
        $this->assertText('Użytkownik już istnieje w bazie.');
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $sth = $this->dbh->prepare("SELECT COUNT(ROWID) FROM auth_user WHERE user = :user");
        $sth -> bindParam(':user',$this->userUsername,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
    }

    public function testAddUserWithWrongLogin()
    {
        //Go to default page
        $this->get($this->url.'?');
        $this->click("Nowy użytkownik");
        $this->assertText('Podaj login i hasło. Użyj działającego adresu e-mail jako login.');
        $this->setFieldByName('username', 'nieadres');
        $this->setFieldByName('password', $this->userPassword);
        $this->setFieldByName('passwordRepeat', $this->userPassword);
        $this->click("Dodaj");
        $this->assertText('Podaj email jako login.');
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $sth = $this->dbh->prepare("SELECT COUNT(ROWID) FROM auth_user WHERE user = :user");
        $sth -> bindParam(':user',$this->userUsername,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],0);
    }

    public function testAddUserWithWrongRepeatedPassword()
    {
        //Go to default page
        $this->get($this->url.'?');
        $this->click("Nowy użytkownik");
        $this->assertText('Podaj login i hasło. Użyj działającego adresu e-mail jako login.');
        $this->setFieldByName('username', $this->userUsername);
        $this->setFieldByName('password', $this->userPassword);
        $this->setFieldByName('passwordRepeat', 'X');
        $this->click("Dodaj");
        $this->assertText('Podane hasła nie są identyczne.');
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $sth = $this->dbh->prepare("SELECT COUNT(ROWID) FROM auth_user WHERE user = :user");
        $sth -> bindParam(':user',$this->userUsername,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],0);
    }


    public function testAddUserWithPasswordNoLogin()
    {
        //Go to default page
        $this->get($this->url.'?');
        $this->click("Nowy użytkownik");
        $this->assertText('Podaj login i hasło. Użyj działającego adresu e-mail jako login.');
        $this->setFieldByName('username', '');
        $this->setFieldByName('password', $this->userPassword);
        $this->setFieldByName('passwordRepeat', $this->userPassword);
        $this->click("Dodaj");
        $this->assertText('Podaj login.');
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $sth = $this->dbh->prepare("SELECT COUNT(ROWID) FROM auth_user WHERE user = :user");
        $sth -> bindParam(':user',$this->userUsername,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],0);
    }

    public function testAddUserWithLoginNoPassword()
    {
        //Go to default page
        $this->get($this->url.'?');
        $this->click("Nowy użytkownik");
        $this->assertText('Podaj login i hasło. Użyj działającego adresu e-mail jako login.');
        $this->setFieldByName('username', $this->userUsername);
        $this->setFieldByName('password', '');
        $this->setFieldByName('passwordRepeat', '');
        $this->click("Dodaj");
        $this->assertText('Podaj hasło.');
        $this->assertText('Powtórz hasło.');
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $sth = $this->dbh->prepare("SELECT COUNT(ROWID) FROM auth_user WHERE user = :user");
        $sth -> bindParam(':user',$this->userUsername,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],0);
    }

    public function testLoginNoData()
    {
        //Go to default page
        $this->get($this->url.'?');
        $this->assertText('Podaj login i hasło!');
        $this->setFieldByName('username', '');
        $this->setFieldByName('password', '');
        $this->click("Login");
        $this->assertText('Podaj login i hasło!');
    }

    public function testLoginOnlyLogin()
    {
        //Go to default page
        $this->get($this->url.'?');
        $this->assertText('Podaj login i hasło!');
        $this->setFieldByName('username', 'w');
        $this->setFieldByName('password', '');
        $this->click("Login");
        $this->assertText('Błędny login lub hasło!');
    }

    public function testLoginOnlyPassword()
    {
        //Go to default page
        $this->get($this->url.'?');
        $this->assertText('Podaj login i hasło!');
        $this->setFieldByName('username', '');
        $this->setFieldByName('password', 'w');
        $this->click("Login");
        $this->assertText('Podaj login i hasło');
    }

    public function testLoginAddUserLoginOnly()
    {
        //Go to default page
        $this->get($this->url.'?');
        $this->click("Nowy użytkownik");
        $this->assertText('Podaj login i hasło. Użyj działającego adresu e-mail jako login.');
        $this->setFieldByName('username', $this->userUsername);
        $this->setFieldByName('password', $this->userPassword);
        $this->setFieldByName('passwordRepeat', $this->userPassword);
        $this->click("Dodaj");
        $this->assertText('Użytkownik dodany.');
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $sth = $this->dbh->prepare("SELECT COUNT(ROWID) FROM auth_user WHERE user = :user");
        $sth -> bindParam(':user',$this->userUsername,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
        $this->get($this->url.'?');
        $this->assertText('Podaj login i hasło!');
        $this->setFieldByName('username',  $this->userUsername);
        $this->setFieldByName('password', '');
        $this->click("Login");
        $this->assertText('Błędny login lub hasło!');

    }

    public function testUserLogin()
    {
        //Go to default page
        $this->get($this->url.'?');
        $this->click("Nowy użytkownik");
        $this->assertText('Podaj login i hasło. Użyj działającego adresu e-mail jako login.');
        $this->setFieldByName('username', $this->userUsername);
        $this->setFieldByName('password', $this->userPassword);
        $this->setFieldByName('passwordRepeat', $this->userPassword);
        $this->click("Dodaj");
        $this->assertText('Użytkownik dodany.');
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $sth = $this->dbh->prepare("SELECT COUNT(ROWID) FROM auth_user WHERE user = :user");
        $sth -> bindParam(':user',$this->userUsername,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
        $this->get($this->url.'?');
        $this->assertText('Podaj login i hasło!');
        $this->setFieldByName('username',  $this->userUsername);
        $this->setFieldByName('password',  $this->userPassword);
        $this->click("Login");
        $this->assertText('Mapa poglądowa oraz lista trofeów');
    }

    public function testUserLoginNoAccessToAdmin()
    {
        //Go to default page
        $this->get($this->url.'?');
        $this->click("Nowy użytkownik");
        $this->assertText('Podaj login i hasło. Użyj działającego adresu e-mail jako login.');
        $this->setFieldByName('username', $this->userUsername);
        $this->setFieldByName('password', $this->userPassword);
        $this->setFieldByName('passwordRepeat', $this->userPassword);
        $this->click("Dodaj");
        $this->assertText('Użytkownik dodany.');
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $sth = $this->dbh->prepare("SELECT COUNT(ROWID) FROM auth_user WHERE user = :user");
        $sth -> bindParam(':user',$this->userUsername,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
        $this->get($this->url.'?');
        $this->assertText('Podaj login i hasło!');
        $this->setFieldByName('username',  $this->userUsername);
        $this->setFieldByName('password',  $this->userPassword);
        $this->click("Login");
        $this->assertText('Mapa poglądowa oraz lista trofeów');
        $this->get($this->url.'?cmd=AdminAddCode');
        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertText('Podaj login i hasło!');
    }


    public function testAdminLogin()
    {
        //Go to default page
        $this->get($this->url.'?cmd=AdminAddCode');
        $this->assertText('Podaj login i hasło!');
        $this->setFieldByName('username',  $this->username);
        $this->setFieldByName('password',  $this->password);
        $this->click("Login");
        $this->assertTitle('Polowanie na kody Administracja');        
        $this->assertText('Dodaj kod');
    }

    public function testAdminLoginNoAccessToUser()
    {
        //Go to default page
        $this->get($this->url.'?cmd=AdminAddCode');
        $this->assertText('Podaj login i hasło!');
        $this->setFieldByName('username',  $this->username);
        $this->setFieldByName('password',  $this->password);
        $this->click("Login");
        $this->assertTitle('Polowanie na kody Administracja');        
        $this->assertText('Dodaj kod');
        $this->get($this->url.'?');
        $this->assertTitle('Polowanie na kody');
        $this->assertText('Podaj login i hasło!');
    }

    public function tearDown() 
    {
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
  
        $this->dbh = null;

        AuthSingleton::instance()->removeUser($this->username);
        $this->get($this->url.'?cmd=AdminCodeList&logout=1');

        AuthSingleton::instance('AUTH_USER')->removeUser($this->userUsername);
        $this->get($this->url.'?cmd=HuntCode&logout=1');        
    }
 

}
?>
